<?php

namespace Admin\Controller;
/**
 * 任务目标
 * @author Dinghua
 *
 */
class ObjectiveController extends AdminController{
	
	/**
	 * 任务列表
	 */
	public function index() {
		$map = array();
	
		$list = $this->lists('Objective', $map, 'id DESC');

		$this->assign('_list', $list);
		
		$this->meta_title = '任务列表';
		
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->display();
	}
	
	
	/**
	 * 添加目任务
	 */
	 public function add() {
		
		$this->meta_title = '添加任务';
		$this->display();
		
	 }
	 
	 /**
	  * 编辑任务目标
	  */
	public function edit($id = NULL) {
		$info = D('Objective')->info($id);
		$this->assign('info', $info);
		$this->meta_title = '编辑任务';
		$this->display('add');
	}
	
	/**
	 * 某个目录查询业务员的业绩
	 */
	public function view($id = NULL) {
		if (empty($id)) $this->error('参数错误！');
		
		$info = M('Objective')->find($id);
		//获取所有业务员
		$uid = M('auth_group_access')->where(array('group_id' => 5))->getField('uid', true);
		$map['uid'] = array('in', $uid);
		$userList = M('Member')->where($map)->select();
		foreach ($userList as $key => $val) {
			
			$where['userid'] = $val['uid'];
			$where['ordtime'][] = array('egt', $info['time-start']);
			$where['ordtime'][] = array('elt',24*60*60 + $info['time-end']);
			$userList[$key]['ordfee'] = M('Order')->where($where)->sum('ordfee');
			$sum += $userList[$key]['ordfee'];
		}
		$this->assign('_list', $userList);
		$this->assign('sum', $sum);
		$this->assign('info', $info);
		$this->meta_title = '目标任务完成进度';
		$this->display();
	}
	
	
	 /**
	 * 更新或新增一条数据
	 */
	public function update(){
	
		$Objective = D('Objective');
		if ( isset($_POST['time-start']) ) {
			$_POST['time-start'] = strtotime(I('time-start'));
		}
		if ( isset($_POST['time-end']) ) {
			$_POST['time-end'] = 24*60*60 + strtotime(I('time-end'));
		}

		$res = $Objective->update();
		if(!$res){
			$this->error($Objective->getError());
		}else{
			$this->success($res['id']?'更新成功':'新增成功', Cookie('__forward__'));
		}
	}
	
	
}